﻿Imports System.Collections.ObjectModel

Public Class Lexicon(Of TTokenType)

    Private m_lexicalTokenList As List(Of LexicalToken)
    Private m_tokenTypeList As List(Of TTokenType)

    Public Sub New()
        m_lexicalTokenList = New List(Of LexicalToken)
        m_tokenTypeList = New List(Of TTokenType)
        '0 means no token type
        m_tokenTypeList.Add(Nothing)
    End Sub

    Public ReadOnly Property LexicalTokens As ReadOnlyCollection(Of LexicalToken)
        Get
            Return m_lexicalTokenList.AsReadOnly()
        End Get
    End Property

    Public ReadOnly Property TokenTypes As ReadOnlyCollection(Of TTokenType)
        Get
            Return m_tokenTypeList.AsReadOnly()
        End Get
    End Property

    Public Function DefineToken(ByVal regex As RegularExpression, ByVal tokenType As TTokenType, Optional ByVal isSkipToken As Boolean = False, Optional ByVal priority As Integer = 0) As LexicalToken
        CodeContract.RequiresArgumentNotNull(regex, "regex")

        Dim tokenTypeIndex = m_tokenTypeList.Count
        m_tokenTypeList.Add(tokenType)
        Dim newLexicalToken As New LexicalToken(regex, tokenTypeIndex, isSkipToken, priority)
        m_lexicalTokenList.Add(newLexicalToken)

        Return newLexicalToken
    End Function

    Public Function CreateSegment(ByVal ParamArray tokens As LexicalToken()) As LexiconSegment
        Return New LexiconSegment(tokens)
    End Function
End Class

